package com.yalemang.photovalut.ui.main.page.photo

import UserManager
import android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import com.yalemang.framework.base.BaseFragment
import com.yalemang.photovalut.App
import com.yalemang.photovalut.CommonCenterDialog
import com.yalemang.photovalut.base.BaseChickAdapter
import com.yalemang.photovalut.base.ext.gone
import com.yalemang.photovalut.base.ext.visible
import com.yalemang.photovalut.base.helper.CommonDrawableHelper
import com.yalemang.photovalut.base.helper.CommonItemDecoration
import com.yalemang.photovalut.base.sql.SQLManager
import com.yalemang.photovalut.base.sql.entity.Folder
import com.yalemang.photovalut.databinding.FragmentPhotoBinding
import com.yalemang.photovalut.ui.folder.PhotoFolderActivity
import com.yalemang.photovalut.ui.main.page.photo.adapter.FolderAdapter

class PhotoFragment : BaseFragment<FragmentPhotoBinding, PhotoPresenter>(),
    PhotoConstract.PhotoView {

    private var folderList = ArrayList<Folder>()
    private lateinit var mFolderAdapter: FolderAdapter
    private val sqlManager = SQLManager.getInstance()
    private var skin = UserManager.instance.user.skin

    override fun initData() {
        folderList = sqlManager.photoFolderManagerTable.allData as ArrayList<Folder>
        mFolderAdapter =
            FolderAdapter(requireActivity(), folderList, mPresenter.mModel.getSkin().mainColor)
    }

    @SuppressLint("CheckResult")
    override fun initView() {
        mBinding.ivPhotoBottom.background =
            CommonDrawableHelper.Builder().setAll(45f).setColor(skin.mainColor).builder()

        if (folderList.size != 0) {
            mBinding.tvPhoto.gone()
            mBinding.ivPhotoCenter.gone()
            mBinding.ivPhotoBottom.visible()
        }

        mFolderAdapter.itemClickListener = { data: Folder, _: Int ->
            val intent = Intent(activity, PhotoFolderActivity::class.java)
            PhotoFolderActivity.folderName = data.folderName
            startActivity(intent)
        }
//        mFolderAdapter.menuClickListener = object : BaseChickAdapter.MenuClickListener {
//            override fun click(isExistFolder: Boolean) {
//                if (!isExistFolder) {
//                    mBinding.tvPhoto.visible()
//                    mBinding.ivPhotoCenter.visible()
//                    mBinding.ivPhotoBottom.gone()
//                }
//            }
//        }

        mBinding.rvPhoto.layoutManager = GridLayoutManager(activity, 2)
        mBinding.rvPhoto.adapter = mFolderAdapter
        mBinding.rvPhoto.addItemDecoration(CommonItemDecoration.getItemDecoration(10))

        mBinding.ivPhotoBottom.setOnClickListener {
            showCenterDialog()
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun showCenterDialog() {
        CommonCenterDialog.Builder().setTitle("123")
            .setSkinColor(UserManager.instance.user.skin.mainColor)
            .setIsEdit(true)
            .setCancelClick {
                Toast.makeText(context, "取消", Toast.LENGTH_SHORT).show()
            }
            .setConfirmClick {
                Toast.makeText(context, "确认", Toast.LENGTH_SHORT).show()

                if (sqlManager.createNewPhotoFolder(it)) {
                    val folder = Folder()
                    folder.folderName = it
                    folderList.add(folder)
                    mFolderAdapter.notifyDataSetChanged()
                } else {
                    Toast.makeText(requireActivity(), "添加失败", Toast.LENGTH_SHORT).show()
                }

                mBinding.tvPhoto.gone()
                mBinding.ivPhotoCenter.gone()
                mBinding.ivPhotoBottom.visible()

                true
            }
            .build().show(requireActivity())
    }

    override fun initMVP() {
        mPresenter = PhotoPresenter()
        mPresenter.mView = this
        mPresenter.mModel = PhotoModel()
    }
}